package cn.stacey.day09;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

public class DemoKeywordsCount {
	private static final String SENTENSE = "abstract 	continue	for	new	switch\r\n"
			+ "assert***	default	goto*	package	synchronized\r\n" + "boolean	do	if	private	this\r\n"
			+ "break	double	implements	protected	throw\r\n" + "byte	else	import	public	throws\r\n"
			+ "case	enum****	instanceof	return	transient\r\n" + "catch	extends	int	short	try\r\n"
			+ "char	final	interface	static	void\r\n" + "class	finally	long	strictfp**	volatile\r\n"
			+ "const*	float	native	super	while\r\n";

	@Test
	public void count() throws IOException {
		// 读取某个文件的内容(一行一行读出来)
		String file = "D:\\yuanshi\\camp2018\\camp\\camp1\\test1\\src\\cn\\stacey\\day08\\DemoListSort.java";
		List<String> lines = Files.readAllLines(Paths.get(file));
		// 所有的关键字,调用getkeywords方法
		Set<String> keywords = getKeyWords();
		// value key
		Map<String, Integer> map = new HashMap<>();

		for (String line : lines) {
			String[] words = line.split("[ \t.?;()<>]");
			// System.out.println(Arrays.toString(words));
			for (String word : words) {
				// System.out.println(word);
				if (keywords.contains(word)) {
					if (map.containsKey(word)) {
						Integer count = map.get(word);
						count++;
						map.put(word, count);
					} else {
						map.put(word, 1);
					}

				}
			}
			
		}
		System.out.println(map);
		
		
		// 遍历map集合,map不能直接遍历，要先转为set

		
	}
	// @Test

	// public void getKeyWords() {
	// //Set<String> result=new HashSet();
	// String[] split=SENTENSE.split("[ \t*]");
	// for(int i=0;i<split.length;i++) {
	// System.out.println(i);
	// System.out.println(split[i]);
	//
	// }
	//
	// }

	public Set<String> getKeyWords() {
		Set<String> result = new HashSet();
		String[] split = SENTENSE.split("[ \t*]");
		for (int i = 0; i < split.length; i++) {
			// System.out.println(i);
			//当前字符不是空的才添加
			if(!"".equals(split[i])) {
				result.add(split[i]);
			}
			
			// System.out.println(result);

		}
		return result;

	}

}
